import React from 'react'
/*
引入store 用于获取redux中保存的状态
*/
import store from "../../store/store"

export default function Count() {
  //点击按钮加减事件
  const { count } = store.getState()
  const increamentHandle = () => {
    store.dispatch({
      type: "increamentCount",
      data: 1
    })
  }

  const decreamentHandle = () => {
    store.dispatch({
      type: "decreamentCount",
      data: 1
    })
  }
  //点击按钮如果是奇数再+1事件
  const ifOddIncreamentHandle = () => {
    if (count % 2 !== 0) {
      store.dispatch({
        type: "increamentCount",
        data: 1
      })
    }
  }

  //点击按钮等2s再加1事件
  const waitIncreamentHandle = () => {
    setTimeout(() => {
      store.dispatch({
        type: "increamentCount",
        data: 1
      })
    },1000)
  }
  return (

    <div>
      <h1>我是count组件 我的值是{count}</h1>
      <div>
        <button onClick={increamentHandle}>累加</button>
        <button onClick={decreamentHandle}>累减</button>
        <button onClick={ifOddIncreamentHandle}>如果是奇数再加1</button>
        <button onClick={waitIncreamentHandle}>等2s再加1</button>
      </div>
    </div>
  )

}





